﻿using SigowayCommonLib;
using SigowayCommonLib.ADO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.Common;

namespace WarehouseManager.View
{
    public partial class DrugListForm : Form
    {
        private DBManager dbManager;
        private string stroperator;
        private string strDateNow;
        private DataTable datSource;

        public DrugListForm(string _operator)
        {
            InitializeComponent();

            stroperator = _operator;
           
        }

        private void DrugListForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.Init();
            }
            catch (Exception ex)
            {
                MessageBoxHelp.ShowMsgError("初期启动失败!");
                LogManager.WriteLine("初期启动失败!Exception={0}", ex);
            }
        }

        private void Init()
        {
            this.dbManager = DBManager.GetDBManager();
            this.lblOperator.Text = this.stroperator;
            this.lblOperatDate.Text = DateTimeUtil.GetCurrentDate();

            datSource = this.CreateDataSource();
            this.dataGridDurgList.DataSource = this.datSource;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dateDurgInfo = null;
                this.datSource.Clear();
                //全检索
                if (string.IsNullOrEmpty(this.txtDrugName.Text.Trim()))
                {
                    var sqlText = "SELECT * FROM DrugInfo ";
                    dateDurgInfo = dbManager.ExecuteQuery(new SQLObject(sqlText, null));
                }
                else
                {
                    var sqlText = string.Format("SELECT * FROM DrugInfo WHERE {0} LIKE ?", ConstInfo.COL_DRUGNAME);
                    IDictionary<string, object> dicParameter = new Dictionary<string, object>();
                    dicParameter.Add(ConstInfo.COL_DRUGNAME, "%" + this.txtDrugName.Text.Trim() + "%");
                    dateDurgInfo = dbManager.ExecuteQuery(new SQLObject(sqlText, dicParameter));
                }
                if (null == dateDurgInfo || dateDurgInfo.Rows.Count < 1)
                {
                    MessageBoxHelp.ShowMsgWarning("药品信息不存在！");
                    txtDrugName.Focus();
                    return;
                }
                this.SetDataSource(dateDurgInfo);
            }
            catch (Exception ex)
            {
                MessageBoxHelp.ShowMsgError("药品信息检索失败!");
                LogManager.WriteLine("药品信息检索失败!Exception={0}", ex);
            }
        }


        private DataTable CreateDataSource()
        {
            DataTable datSource = new DataTable();
            datSource.Columns.Add(ConstInfo.COL_DRUGID, typeof (string));
            datSource.Columns.Add(ConstInfo.COL_DRUGNAME, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_ARTNO, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_UNIT, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_DRUGSPEC, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_PRODUCEAREA, typeof(string));
            datSource.AcceptChanges();
            return datSource;
        }

        private void SetDataSource(DataTable datDurgInfo)
        {
            if (datDurgInfo == null || datDurgInfo.Rows.Count == 0)
            {
                return;
            }

            foreach (DataRow row in datDurgInfo.Rows)
            {
                DataRow rowTemp = this.datSource.NewRow();
                rowTemp[ConstInfo.COL_DRUGID] = row[ConstInfo.COL_DRUGID];
                rowTemp[ConstInfo.COL_DRUGNAME] = row[ConstInfo.COL_DRUGNAME];
                rowTemp[ConstInfo.COL_ARTNO] = row[ConstInfo.COL_ARTNO];
                rowTemp[ConstInfo.COL_UNIT] = row[ConstInfo.COL_UNIT];
                rowTemp[ConstInfo.COL_DRUGSPEC] = row[ConstInfo.COL_DRUGSPEC];
                rowTemp[ConstInfo.COL_PRODUCEAREA] = row[ConstInfo.COL_PRODUCEAREA];
                this.datSource.Rows.Add(rowTemp);
            }
            this.datSource.AcceptChanges();
        }

        private void txtDrugName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)37)
            {
                e.Handled = true;
            }
        }

       
    }
}
